{% extends 'layouts/default.html' %}
{% set title = 'Simple CRM' %}
{% block style %}
{% endblock style %}
{% block container %}
<div class="container-xl">
    <div class="row">
        <div class="col-lg-8 d-flex flex-column">
            <div class="d-flex flex-column mb-5">
                <div class="h4 mb-3">基础数据</div>
                <div class="d-flex flex-wrap justify-content-between">
                    <div class="statistic ms-0 rounded mb-1 mb-lg-0">
                        <div class="d-flex justify-content-between align-items-baseline">
                            <div class="h4 text-dark">员工数量</div>
                            <div class="h6"></div>
                        </div>
                        <div class="fw-bold fs-2">{{data.count.user}}</div>
                    </div>
                    <div class="statistic rounded mb-1 mb-lg-0">
                        <div class="d-flex justify-content-between align-items-baseline">
                            <div class="h4 text-warning">客户数量</div>
                            <div class="h6"></div>
                        </div>
                        <div class="fw-bold fs-2">{{data.count.customer}}</div>
                    </div>
                    <div class="statistic rounded mb-1 mb-lg-0">
                        <div class="d-flex justify-content-between align-items-baseline">
                            <div class="h4 text-info">合同数量</div>
                            <div class="h6"></div>
                        </div>
                        <div class="fw-bold fs-2">{{data.count.contract}}</div>
                    </div>
                    <div class="statistic rounded mb-1 mb-lg-0">
                        <div class="d-flex justify-content-between align-items-baseline">
                            <div class="h4 text-success">产品数量</div>
                            <div class="h6"></div>
                        </div>
                        <div class="fw-bold fs-2">{{data.count.product}}</div>
                    </div>
                </div>
            </div>
            <div class="d-flex flex-column mb-5">
                <div class="h4 mb-3">客户数据</div>
                <div class="row">
                    <div class="col-lg-9 mb-lg-0 mb-3" style="min-height: 300px;">
                        <div class="d-flex w-100 h-100 panel">
                            <div id="funnel" class="funnel w-100 text-dark"></div>
                        </div>
                    </div>
                    <div class="col-lg-3 d-flex flex-column">
                        <div class="statistic rounded mb-1">
                            <div class="d-flex justify-content-between align-items-baseline">
                                <div class="h4">待联系</div>
                                <div class="h6"></div>
                            </div>
                            <div class="fw-bold fs-2">{{data.count.unLink}}</div>
                        </div>
                        <div class="statistic rounded mb-1">
                            <div class="d-flex justify-content-between align-items-baseline">
                                <div class="h4">联系中</div>
                                <div class="h6"></div>
                            </div>
                            <div class="fw-bold fs-2">{{data.count.linking}}</div>
                        </div>
                        <div class="statistic rounded">
                            <div class="d-flex justify-content-between align-items-baseline">
                                <div class="h4">已下单</div>
                                <div class="h6"></div>
                            </div>
                            <div class="fw-bold fs-2">{{data.count.sold}}</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="d-flex flex-column mb-5">
                <div class="mb-2 d-flex justify-content-between align-items-baseline">
                    <div class="h4 mb-0">我的动态</div>
                    <div class="fs-sm text-muted cursor-pointer" onclick="window.location.href='/activities'">更多
                    </div>
                </div>
                <div id="activities" class="mt-2">
                    <div class="text-center">
                        <div class="spinner-border"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-4 d-flex flex-column">
            <div class="d-flex flex-column mb-5">
                <div class="h4 mb-3">合同数据</div>
                <div class="d-flex panel mb-3">
                    <div id="contractsBar" class="w-100 p-4">
                        <div class="d-flex align-items-center justify-content-center" style="height:200px;">
                            <div class="spinner-border"></div>
                        </div>
                    </div>
                </div>
                <div id="receivableBar">
                    <div class="statistic-narrow rounded mb-3 justify-content-between placeholder-wave"
                        style="height: 64px;">
                        <div class="w-100 h-100 placeholder"></div>
                    </div>
                    <div class="statistic-narrow rounded mb-3 justify-content-between placeholder-wave"
                        style="height: 64px;">
                        <div class="w-100 h-100 placeholder"></div>
                    </div>
                    <div class="progress progress-fat placeholder-wave">
                        <div class="w-100 h-100 placeholder"></div>
                    </div>
                </div>
            </div>
            <div class="d-flex flex-column mb-5">
                <div class="mb-2 d-flex justify-content-between align-items-baseline">
                    <div class="h4 mb-0">畅销产品</div>
                </div>
                <div id="productRank" class="mt-2">
                    <ul class="list-group">
                        <li class="list-group-item">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="d-flex flex-column w-100">
                                    <div class="placeholder-wave w-50">
                                        <div class="placeholder col-12"></div>
                                    </div>
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                </div>
                            </div>
                        </li>
                        <li class="list-group-item">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="d-flex flex-column w-100">
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                </div>
                            </div>
                        </li>
                        <li class="list-group-item">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="d-flex flex-column w-100">
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="d-flex flex-column mb-5">
                <div class="mb-2 d-flex justify-content-between align-items-baseline">
                    <div class="h4 mb-0">我的客户</div>
                    <div class="fs-sm text-muted cursor-pointer" onclick="window.location.href='/customers/my'">更多</div>
                </div>
                <div id="myCustomers" class="mt-2">
                    <ul class="list-group">
                        <li class="list-group-item">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="d-flex flex-column w-100">
                                    <div class="placeholder-wave w-50">
                                        <div class="placeholder col-12"></div>
                                    </div>
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                </div>
                            </div>
                        </li>
                        <li class="list-group-item">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="d-flex flex-column w-100">
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                </div>
                            </div>
                        </li>
                        <li class="list-group-item">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="d-flex flex-column w-100">
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                    <div class="placeholder-wave w-50"><span class="placeholder col-12"></span></div>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

</div>
{% endblock container %}
{% block script %}
<script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>
<script src="https://unpkg.com/@antv/g2"></script>
<script src="https://unpkg.com/@antv/data-set"></script>
<script src="public/js/activities.js"></script>
<script>
    const app = {
        async renderActivities() {
            const container = document.getElementById('activities');
            $.ajax({
                type: 'get',
                url: '/activities/my',
                data: { limit: 10 }
            }).done(function (result) {
                const { data } = result;
                container.innerHTML = activities.render({ isMy: true, data });
            }).fail(function () {
                container.innerHTML = '';
            });
        },
        async renderProductRank() {
            const container = document.getElementById('productRank');
            $.ajax({
                type: 'get',
                url: '/statistic/products/rank/top',
                data: { limit: 3 }
            }).done(function (result) {
                const { data } = result;
                let row = '';
                for (let i = 0; i < data.length; i++) {
                    const p = data[i];
                    row += `
                        <li class="list-group-item">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="d-flex flex-column">
                                    <div>${p.name}</div>
                                    <div class="text-muted">总销售：¥${p.totalSales}</div>
                                </div>
                                <div class="badge text-bg-dark">${i + 1}</div>
                            </div>
                        </li>
                    `;
                }
                let html = `
                    <ul class="list-group">
                        ${row}
                    </ul>
                `;
                container.innerHTML = html;
            }).fail(function () {
                container.innerHTML = '';
            });
        },
        async renderReceivableBar() {
            const container = document.getElementById('receivableBar');
            $.ajax({
                type: 'get',
                url: '/statistic/contracts/receivable',
            }).done(function (result) {
                let { data } = result;
                const percent = (Number(data.done) || 0) / (Number(data.total) || 1) * 100;
                container.innerHTML = `
                    <div class="statistic-narrow rounded mb-3 justify-content-between">
                        <div class="fs-5">回款总金额</div>
                        <div class="fs-5 ms-2">¥${data.done || 0}</div>
                    </div>
                    <div class="statistic-narrow rounded mb-3 justify-content-between">
                        <div class="fs-5">合同总金额</div>
                        <div class="fs-5 ms-2">¥${data.total || 0}</div>
                    </div>
                    <div class="progress progress-fat">
                        <div class="progress-bar progress-bar-striped progress-bar-animated bg-dark" role="progressbar"
                            aria-label="Basic example" style="width: ${percent.toFixed(2)}%" aria-valuenow="50" aria-valuemin="0"
                            aria-valuemax="100">
                            <span class="fw-bold">${percent.toFixed(2)}%</span>
                        </div>
                    </div>
                `;
            }).fail(function () {
                container.innerHTML = '';
            });
        },
        async renderContractsChart() {
            const container = document.getElementById('contractsBar');
            $.ajax({
                type: 'get',
                url: '/statistic/contracts/last7days',
            }).done(function (result) {
                container.innerHTML = '';
                let { data } = result;
                data = Object.keys(data).map(day => {
                    return {
                        name: '合同',
                        日期: dayjs(day).format('MM-DD'),
                        数量: data[day],
                    };
                });
                const chart = new G2.Chart({
                    container: 'contractsBar',
                    autoFit: true,
                    height: 200,
                });
                chart.data(data).legend(false);
                chart.tooltip({ showMarkers: false, shared: false, });
                chart.interval().position('日期*数量').color('name', ['#000', '#555'])
                chart.render();
            }).fail(function () {
                container.innerHTML = '';
            });
        },
        async renderMyCustomers() {
            const container = document.getElementById('myCustomers');
            $.ajax({
                type: 'get',
                url: '/customers/search',
                data: { hide: true, my: true, limit: 10 }
            }).done(function (result) {
                const { data } = result;
                let row = '';
                for (const u of data) {
                    row += `
                        <li class="list-group-item cursor-pointer" onclick="window.location.href='/customers/${u.id}'">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="d-flex flex-column">
                                    <div>${u.name}</div>
                                    <div>${u.phone}</div>
                                </div>
                                <div class="badge" style="background-color: ${u.stage?.colorHex || '#212529'};">${u.stage?.name || '未知'}</div>
                            </div>
                        </li>
                    `;
                }
                let html = `
                    <ul class="list-group">
                        ${row}
                    </ul>
                `;
                container.innerHTML = html;
            }).fail(function () {
                container.innerHTML = '';
            });
        },
        async renderFunnel() {
            const { DataView } = DataSet;
            const dv = new DataView().source([
                { action: '待联系', pv: Number('{{data.count.unLink}}') },
                { action: '联系中', pv: Number('{{data.count.linking}}') },
                { action: '已下单', pv: Number('{{data.count.sold}}') },
            ]);
            const total = Number('{{data.count.unLink}}') + Number('{{data.count.linking}}') + Number('{{data.count.sold}}');
            dv.transform({
                type: 'map',
                callback(row) {
                    row.percent = row.pv / total;
                    return row;
                },
            });
            const data = dv.rows;
            const chart = new G2.Chart({
                container: 'funnel',
                autoFit: true,
                padding: [20, 30, 20],
            });
            chart.data(data);
            chart.axis(false);
            chart.legend(false);
            chart.tooltip({
                showTitle: false,
                showMarkers: false,
                itemTpl:
                    '<li style="margin-bottom:4px;list-style-type:none;padding: 0;">' +
                    '<span style="background-color:{color};" class="g2-tooltip-marker"></span>' +
                    '{name}<br/>' +
                    '<span style="padding-left: 16px;line-height: 16px;">浏览人数：{pv}</span><br/>' +
                    '<span style="padding-left: 16px;line-height: 16px;">占比：{percent}</span><br/>' +
                    '</li>',
            });
            chart
                .coordinate('rect')
                .transpose()
                .scale(1, -1);
            chart
                .interval()
                .adjust('symmetric')
                .position('action*percent')
                .shape('funnel')
                .color('action', ['#000', '#333', '#777'])
                .tooltip('action*pv*percent', (action, pv, percent) => {
                    return {
                        name: action,
                        percent: (+percent * 100).toFixed(2) + '%',
                        pv,
                    };
                })
                .animate({
                    appear: { animation: 'fade-in' },
                    update: { annotation: 'fade-in' }
                });

            chart.interaction('element-active');
            chart.on('beforepaint', () => {
                chart.annotation().clear(true);
                const chartData = chart.getData();
                // 中间标签文本
                chartData.forEach((obj) => {
                    chart.annotation().text({
                        top: true,
                        position: {
                            action: obj.action,
                            percent: 'center',
                        },
                        content: (+obj.percent * 100).toFixed(2) + '%', // 显示的文本内容
                        style: {
                            stroke: null,
                            fill: '#fff',
                            textAlign: 'center',
                        },
                    });
                });
            });
            chart.render();
        }
    };
    $(document).ready(async function () {
        app.renderActivities();
        app.renderMyCustomers();
        app.renderContractsChart();
        app.renderReceivableBar();
        app.renderFunnel();
        app.renderProductRank();
    });
</script>
{% endblock script %}